package com.xzqwjw.taskmanager.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class GridItemLeaderInfoAdapter extends BaseAdapter {

  private LayoutInflater objLInflater;
  private List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
  @SuppressWarnings("unused")
  private Bitmap bmpNoPhoto = null;
  private final Context objContext;
  private String strDBFile;

  public GridItemLeaderInfoAdapter(Context objContext, List<Map<String, Object>> listData, String strDBFile) {
    this.objContext = objContext;
    this.strDBFile = strDBFile;
    this.objLInflater = (LayoutInflater) objContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.listData = listData;
    bmpNoPhoto = Utility.getBMPFromRES(objContext, R.drawable.nophoto);
  }

  @Override
  public Object getItem(int intPosition) {
    if (listData == null)
      return null;
    else
      return listData.get(intPosition);
  }

  @Override
  public long getItemId(int intPosition) {
    return intPosition;
  }

  @Override
  public int getCount() {
    return this.listData != null ? this.listData.size() : 0;
  }

  @Override
  public View getView(int intPosition, View viewConvert, ViewGroup vgParent) {
    if (listData == null) {
      return null;
    }

    final ViewGridItem objViewHolder;

    if (viewConvert == null) {
      objViewHolder = new ViewGridItem();
      viewConvert = objLInflater.inflate(R.layout.item_gridview_unit,
          null);

      objViewHolder.tvID = (TextView) viewConvert.findViewById(R.id.tvID);
      objViewHolder.imgbtnHeadImg = (ImageButton) viewConvert
          .findViewById(R.id.imgbtnHeadPhoto);
      objViewHolder.tvName = (TextView) viewConvert
          .findViewById(R.id.tvName);
      objViewHolder.tvSex = (TextView) viewConvert
          .findViewById(R.id.tvSex);
      objViewHolder.tvBirthdayAge = (TextView) viewConvert
          .findViewById(R.id.tvBirthdayAge);
      objViewHolder.tvNativePlace = (TextView) viewConvert
          .findViewById(R.id.tvNativePlace);
      objViewHolder.imgbtnFavorite = (ImageButton) viewConvert
          .findViewById(R.id.imgbtnFavorite);
      viewConvert.setTag(objViewHolder);
    } else {
      objViewHolder = (ViewGridItem) viewConvert.getTag();
    }

    final String strAID = (String) listData.get(intPosition).get("AID");
    final int intAID = Integer.parseInt(strAID);
    objViewHolder.tvID.setText(strAID);

    String strPhotoPath = (String) listData.get(intPosition).get("Photo");
    Bitmap bmpPhoto = BitmapFactory.decodeFile(strPhotoPath, null);
    objViewHolder.imgbtnHeadImg.setImageBitmap(Utility.zoomBMP(bmpPhoto,
        120, 160));

    objViewHolder.imgbtnHeadImg
        .setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent objIT = new Intent(objContext,
                ActivityShowInfo.class);
            objIT.putExtra("AID", strAID);
            objIT.putExtra("DBFilePath", strDBFile);
            objContext.startActivity(objIT);
          }
        });

    objViewHolder.tvName.setText((String) listData.get(intPosition).get(
        "RealName"));

    objViewHolder.tvSex.setText((String) listData.get(intPosition).get(
        "Sex"));

    objViewHolder.tvBirthdayAge.setText((String) listData.get(intPosition)
        .get("BirthdayAge"));

    objViewHolder.tvNativePlace.setText((String) listData.get(intPosition)
        .get("NativePlace"));

    final com.leaderinfo.dao.PartyInfoDao objDaoPartyInfo = new com.leaderinfo.dao.PartyInfoDao(strDBFile);
    if (objDaoPartyInfo.getFavoriteFlag(intAID) == 0) {
      objViewHolder.imgbtnFavorite.setImageDrawable(objContext
          .getResources().getDrawable(R.drawable.bgbtn_favorite_add));
    } else {
      objViewHolder.imgbtnFavorite.setImageDrawable(objContext
          .getResources().getDrawable(
              R.drawable.bgbtn_favorite_cancel));
    }

    objViewHolder.imgbtnFavorite
        .setOnClickListener(new ImageButton.OnClickListener() {
          @Override
          public void onClick(View v) {
            int intResult = 0;
            intResult = objDaoPartyInfo.setFavorite(intAID);
            if (intResult > 0) {
              if (objDaoPartyInfo.getFavoriteFlag(intAID) == 0) {
                objViewHolder.imgbtnFavorite
                    .setImageDrawable(objContext
                        .getResources()
                        .getDrawable(
                            R.drawable.bgbtn_favorite_add));
              } else {
                objViewHolder.imgbtnFavorite
                    .setImageDrawable(objContext
                        .getResources()
                        .getDrawable(
                            R.drawable.bgbtn_favorite_cancel));
              }
              notifyDataSetChanged();
            }

          }
        });

    return viewConvert;
  }
}